use ${dirTemp}/compustat_quarterly_prepared, clear

* Compute markups 
* =======================

* PF baseline
merge m:1 naicsq2 date_q using ${dirTemp}/pf_CD_baseline, keep(1 3) nogen
gen lmarkup_PF = log(thetaCogsCD_baseline * saleq / cogsq) if baseline == 1
la var lmarkup_PF "PF markup"

* PF baseline incl. SGA
merge m:1 naicsq2 date_q using ${dirTemp}/pf_CD_baseline_sga, keep(1 3) nogen
gen lmarkup_PF_sga = log(thetaCogsCD_baseline_sga * saleq / (cogsq+xsgaq)) if baseline == 1
la var lmarkup_PF_sga "PF markup incl. sga"

* PF lowsales 
merge m:1 naicsq2 date_q using ${dirTemp}/pf_CD_lowsales, keep(1 3) nogen
gen lmarkup_PF_lowsales = log(thetaCogsCD_lowsales * saleq / cogsq) if lowsales == 1
la var lmarkup_PF_lowsales "PF markup lowsales"

* PF excgro 
merge m:1 naicsq2 date_q using ${dirTemp}/pf_CD_excgro, keep(1 3) nogen
gen lmarkup_PF_excgro = log(thetaCogsCD_excgro * saleq / cogsq) if excgro == 1
la var lmarkup_PF_excgro "PF markup excgro"

* PF min16
merge m:1 naicsq2 date_q using ${dirTemp}/pf_CD_min16, keep(1 3) nogen
gen lmarkup_PF_min16 = log(thetaCogsCD_min16 * saleq / cogsq) if min16 == 1
la var lmarkup_PF_min16 "PF markup min16"

* PF Translog (TL) with firm-quarter specific elasticity
merge m:1 naicsq4 using ${dirTemp}/pf_TL_baseline, keep(1 3) nogen
gen elaCogsTL = betaCogsTL_baseline + 2*betaCogs2TL_baseline*log(cogsq)
gen lmarkup_PF_TL = log(elaCogsTL * saleq / cogsq) if baseline == 1
la var lmarkup_PF_TL "PF TL markup"

* PF markup with 4d-industry-quarter-constant elasticity estimated through cost share
gen CS = cogsq/(cogsq+0.12/4*ppeitq) if baseline == 1
bysort naicsq4 date_q: egen median_CS = median(CS)
gen lmarkup_PF_CS = log(median_CS * saleq / cogsq) if baseline == 1
drop CS median_CS
la var lmarkup_PF_CS "PF CS markup"

* Accounting profits markup
gen lmarkup_AP = log(saleq / (cogsq + xsgaq + dpq)) if baseline == 1
la var lmarkup_AP "AP markup"

* User cost markup
gen lmarkup_UC = log(saleq / (cogsq + xsgaq + dpq + ppeitq * rk)) if baseline == 1
la var lmarkup_UC "UC markup"  

* Outlier treatments
* ===========================
gen lmarkup_PF_tr1 = lmarkup_PF

* 5% trimming for baseline markups
bysort date_q: egen plo = pctile(lmarkup_PF), p(5)
bysort date_q: egen pup = pctile(lmarkup_PF), p(95)

gen trim = 0
replace trim = 1 if lmarkup_PF > pup
replace trim = 1 if lmarkup_PF < plo

replace lmarkup_PF = . if trim == 1

drop pup plo

* Drop accordingly non-baseline markups
foreach m of varlist lmarkup_PF_sga lmarkup_PF_TL lmarkup_PF_CS lmarkup_AP lmarkup_UC {
	replace `m' = . if trim == 1
}
	
* Drop other markups
foreach m of varlist lmarkup_PF_lowsales lmarkup_PF_excgro lmarkup_PF_min16 { 
	bysort date_q: egen plo = pctile(`m'), p(5)
	bysort date_q: egen pup = pctile(`m'), p(95)

	replace `m'=. if `m' > pup
	replace `m'=. if `m' < plo
	
	drop pup plo
}

* Alternative 1% trimming
bysort date_q: egen plo = pctile(lmarkup_PF_tr1), p(1)
bysort date_q: egen pup = pctile(lmarkup_PF_tr1), p(99)

replace lmarkup_PF_tr1=. if lmarkup_PF_tr1 > pup
replace lmarkup_PF_tr1=. if lmarkup_PF_tr1 < plo

gen tr1 = baseline
replace tr1 = 1 if lmarkup_PF_tr1 == .

drop pup plo

* Additional dropping on non-baseline markups
foreach m of varlist lmarkup_PF_sga lmarkup_PF_TL lmarkup_PF_CS lmarkup_AP lmarkup_UC {
	bysort date_q: egen plo = pctile(`m'), p(1)
	bysort date_q: egen pup = pctile(`m'), p(99)

	replace `m'=. if `m' > pup
	replace `m'=. if `m' < plo
	
	drop pup plo
}   
 
* Compute market shares 
* =========================
forvalues k=2(2)4 {
	gen aux = saleq if lmarkup_PF < . 
	bysort naicsq`k' date_q: egen totsaleq1`k' = sum(aux) 
	gen mshare`k' = saleq/totsaleq1`k' if naicsq`k'<. & lmarkup_PF < . 
	drop aux 
} 

foreach m of varlist mshare* {
	bysort date_q: egen plo = pctile(`m'), p(1)
	bysort date_q: egen pup = pctile(`m'), p(99)
	gen `m't = `m' if `m' <= pup & `m' >= plo
	drop pup plo
}

* Housekeeping
* ================
keep id date_quarterly naicsq2 naicsq4 saleq cogsq ppeitq dlcq dlttq atq cheq lmarkup* mshare*t freq_avg3 dur_avg3 baseline lowsales excgro min16 tr1

* Save 
* ========= 
save ${dirTemp}/compustat_quarterly_markups, replace
